package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Component
@RequestMapping("/admin/dish")
@RestController
public class DishController {
    @Autowired
    private DishService dishService;

    @GetMapping("/page")
    public Result page(DishPageQueryDTO dishPageQueryDTO) {
        PageResult dishPage = dishService.queryPage(dishPageQueryDTO);
        Result<PageResult> success = Result.success(dishPage);
        return success;
    }

    @PostMapping
    public Result addDish(@RequestBody DishDTO dishDTO) {
        dishService.addDishWithFalvor(dishDTO);
        return Result.success();
    }

    @DeleteMapping
    public Result deleteDishes(@RequestParam Long[] ids) {
        dishService.deleteDishes(ids);
        return Result.success();
    }

    @GetMapping("{id}")
    public Result<DishVO> getDish(@PathVariable Long id) {
        DishVO dishVO = dishService.getDishVO(id);
        return Result.success(dishVO);
    }

    @PutMapping
    public Result updateDish(@RequestBody DishVO dishVO) {
        dishService.updateDishAndFalvors(dishVO);
        return Result.success();
    }

    @GetMapping("list")
    public Result<List<Dish>> querydishlistByCategoryId(Long categoryId) {
        List<Dish> dishList = dishService.querydishlistByCategoryId(categoryId);
        return Result.success(dishList);
    }
}
